package app.controllers.admin.stat;

import app.kits.CalendarKit;
import app.models.game.Game;
import app.models.game.GameJoinrecord;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-04-24 23:04
 * @since JDK 1.6
 */
public class GameController extends BasicController {

    public void index() {
        List<Game> games = Game.dao.list();


        String start = CalendarKit.getFirstDayOfMonth(DateKit.dateFormat);
        String end = CalendarKit.getLastDayOfMonth(DateKit.dateFormat);

        List<Record> joinStats = Lists.newArrayList();
        if (games != null && !games.isEmpty()) {
            for (Game game : games) {
                Record joinStat = GameJoinrecord.dao.joinStat(game, start, end);
                joinStats.add(joinStat);
            }
        }
        setAttr("joinStats", joinStats);

        setAttr("games", games);
        setAttr("start", start);
        setAttr("end", end);
        render("index.ftl");
    }

    public void joinStat() {
        int gameId = getParaToInt("gameId", 0);
        String start = getPara("start");
        String end = getPara("end");

        Record joinStat = GameJoinrecord.dao.joinStat(gameId, start, end);

        renderJson(AjaxMessage.ok(joinStat));
    }

    public void joinStatList() {
        String start = getPara("start");
        String end = getPara("end");
        int game = getParaToInt("game", 0);
        Boolean winning = getParaToBoolean("winning");
        String gift = getPara("gift");

        final DatatablesCriterias criterias = getCriterias();

        Page<Record> page = GameJoinrecord.dao.joinStatList(criterias, start, end, game, winning, gift);
        renderDataTables(page, criterias);
    }


    //----- Deprecated ----
    public void dateGameJoin() {
        String date = getPara("date");
        int game = getParaToInt("game", 0);

        if (!Strings.isNullOrEmpty(date)) {
            date = DateTime.parse(date, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")).toString("yyyy-MM-dd");
        }

        final DatatablesCriterias criterias = getCriterias();
        Page<GameJoinrecord> page = GameJoinrecord.dao.dateGameJoin(criterias, date, game);
        renderDataTables(page, criterias);
    }

    public void detail() {
        int gameId = getParaToInt("id", 0);
        String date = getPara("date");
        Game game = Game.dao.findById(gameId);

        setAttr("game", game);
        setAttr("date", date);
        render("detail.ftl");
    }

    public void detailList() {
        int gameId = getParaToInt("gameId", 0);
        String date = getPara("date");

        String query = getPara("query");
        Boolean winning = getParaToBoolean("winning");

        final DatatablesCriterias criterias = getCriterias();
        Page<GameJoinrecord> page = GameJoinrecord.dao.detailList(criterias, date, gameId, query, winning);
        renderDataTables(page, criterias);
    }

}
